WELCOME! This file (GWBT01.TXT) is the first in what I hope will be a long and enjoyable series of 'programming classes' that will aid you in learning some of the beginner and intermediate level features of the BASIC programming language. First, let me introduce myself. I am Richard Harper, professionally a paramedic in northwest Michigan, but for the last 16 years I have been involved with, and interested in, computers and programming. I have a few Shareware utilities that I currently am marketing, both written entirely in the QuickBASIC programming language. Please keep in mind that the generic term 'BASIC language' can actually refer to one of several flavors. IBM PC and XT users are familiar with the 'BASIC' and 'BASICA' in their ROM's. Users of 'compatible' systems that use MS-DOS are familiar with the GWBASIC.EXE form of Basic. Others use the Professional BASIC and QuickBASIC languages from Microsoft, while some use the TurboBASIC package from Borland. When I refer to 'BASIC' in this series, I will be indicating either BASICA or GWBASIC, unless noted otherwise. The examples presented in this series will require the use of either the BASICA or GWBASIC packages. For those following the series using either QuickBASIC, Professional BASIC or TurboBASIC, please refer to the manuals that came with your software for any specific changes that might need to be made in the programs as presented here to fit them to your system. Each tutorial is intended to serve as a stand-alone module. Although we will be building on each tutorial as we go, you should be able to pick up any one, in any order, and learn from it. We will start by discussing one (or more related) commmand(s) from BASIC, showing some simple programs that demonstrate the topic under discussion, then end the tutorial with a 'homework assignment' that will lead you in the direction of next month's tutorial topic. Of course, you might have some questions regarding the material presented. If so, I would encourage you to post them in the IBM New Users/Fun Forum (GO IBMNEW) on CompuServe, in Message area #0 (General/New Uploads) either to me directly (Richard Harper, User ID 76670,110) or in a message to ALL. If you prefer, you could send messages by CompuServe Mail, but then no one else will 'hear' your question, nor the answer. I will be checking for questions, comments or other input on at least a weekly basis (usually on Wednesdays and/or Saturdays) to try to keep answers coming to you on a timely basis. By posting public messages, we can closely approach a 'classroom' type enviromnent where everyone can participate in questions and discussions on the topics of each lesson using the message areas. Right now, you might be asking yourself why you should bother to learn programming at all. 'Hey, hasn't someone somewhere already written a program that works better than the ones I might write?', you might ask... The answer is: Probably NOT! With a reasonable grasp of the BASIC language, it is easy to write many programs that perform relatively complex tasks, and the program YOU write will be expressely tailored to YOUR preferences. Why settle for a choice from a menu, when you can cook it yourself? Computer programming using BASIC is not difficult at all. In fact, if you can speak English you already know at least half of the BASIC language, and this half includes the most useful 90% of the commands available from BASIC. Ready? Let's dig into our first topic: ******************************************************************************* GW-BASIC TUTORIAL PART ONE: VARIABLES, INPUT AND OUTPUT ******************************************************************************* In order to write a 'useful' program, we need to examine ways to do three things. First, we need a way for the user to enter some sort of information to the program. That information might be a name, number, or some other type of information that the program needs to operate. Second, the computer will usually process this information in some manner. We might add a series of numbers, add to the name provided by a user, or so on. Finally, we need some way of showing this information (after processing it) to the user. Fortunately, BASIC provides very simple ways of doing the inputting and outputting of information, and a number of ways to store and process the information. Before we can ask for information, process it, or show it to the user, we need to understand how BASIC stores such information. The BASIC language provides what are called 'variables' to obtain, store and process information. A variable is simply a name that you give to BASIC and tell it what type of information is being stored. The rules for naming variables are very few and quite simple: 1) BASIC variables must start with an alphabetic letter (A-Z). 2) BASIC variables can consist of any combination of letters (A-Z) or numbers (0-9) following the initial letter, but only the first 40 characters are recognized. Punctuation marks are not allowed. 3) You cannot use BASIC keywords (commands) as a name for a variable, as this would confuse BASIC as to whether the variable is a varible or a command! 4) Some variables will require an optional type identifier as the last character, as follows: Integer numeric variables % Single precision numeric variables ! Double precision numeric variables # String (character) variables $ Don't get TOO upset about the four types listed above, as our introductory sessions will deal with only two of the above: the single precision and string variable types. STRING VARIABLES are used to hold data that you want to represent strictly as the exact characters typed in. A string variable is identified by a dollar sign ($) at the end of the variable name. In addition, if a string variable is given a value within the program, it MUST have quote marks around it. SINGLE PRECISION VARIABLES are used to hold data that is numeric in nature. You MUST use a numeric variable if you want to perform arithmetic operations on the data, such as addition, multiplication, etc. To show the difference between the different variable types, start up BASIC (if you aren't sure how to start BASIC on your system, check your user manuals and/or the BASIC manual that came with your computer for details on how to start and exit from BASIC) and type in the following lines as shown: QUANTITY$ = "3" PRICE$ = "21.45" PRINT QUANTITY * PRICE What happened? The first two lines seemed OK (in fact, BASIC probably said 'OK' after them), but on the third line, BASIC said that this was a 'Type mismatch' error! This is because you cannot multiply string variables. Try this instead (type it in after you've tried the first three lines): PRINT PRICE + QUANTITY What do you see? You always thought 3 + 21.45 was 24.45, right? Not with strings! BASIC interprets the strings exactly as typed in, and in adding them simply combines one string after the other. Now try this: QUANTITY = 3 PRICE = 21.45 PRINT QUANTITY * PRICE PRINT PRICE + QUANTITY Well, it worked all right this time, exactly as you would expect. Unless you add one of the variable type characters ($ # % !) at the end of a variable name, BASIC assumes it is a single-precision numeric variable. As we are using the default single-precision numeric variable types, you need only remember for now these two rules: 1) If a variable will contain alphabetic characters, or is to be interpreted as exactly what the user typed in, or needs to have no arithmetic done on it, add a dollar sign ($) to the end and make it a STRING VARIABLE; 2) If a variable is to be used in arithmetic, or needs to have a number stored in it, put no sign on the end and it is a SINGLE PRECISION VARIABLE. I suggest that you choose names that describe the value being stored, so that you (or others) can later tell what the variable was intended to store. For example: A variable to store an Employee ID number might be called: EMPLOYEEID A variable to store a first name might be called: FIRSTNAME$ A variable to store an item count might be called: COUNT Enough on variables for now. Let's dig in and write a program! Assume you want to ask a user for his/her name, then print a nice little welcome message. In analyzing the program, we see we'll need to use three steps in it: Ask the user for his/her name | V Add the user's name to a welcome message | V Show the welcome message on the screen The above diagram is a 'flowchart', and shows the direction that the program will be moving in. It doesn't hurt to get into the practice of making up flowcharts to help you understand how a program will operate, and they can provide valuable reminders later when you need to get bugs out of your own programs. To ask the user for his/her name, we will use the INPUT statement from BASIC. INPUT prints a question mark on the screen, then pauses while the user puts in the desired information, pressing the Enter key to signal that all the needed information has been provided. In its simplest form, it is used as: INPUT variable-name where variable-name is the type of variable to store the input. You must determine whether the input is to be a string variable (characters) or numeric variable (numbers), and use that type of variable for the input value. You can also use input to provide some information for the user prior to getting the variable. That form of the INPUT statement is: INPUT "text to be printed",variable-name Note that the "text to be printed" can be anything you can put between quotation marks. For example, in our first step of our program, we want the user's name, so it would be logical to ask the user what his/her name is. We would therfore use the statement: INPUT "What is your name, please"; NAME$ You see that I have used a variable called NAME$ to store the name. NAME is a pretty descriptive word, indicating that a name is stored here, and the dollar sign ($) at the end indicates that it is a string variable (holds character- based data as opposed to numbers). You could just as well use the variables USERNAME$, or NAMEOFUSER$, or any other variable name that will remind you what is being stored there. Next, we wanted to add this name to a welcome message to the user. Let's assume that we want to print "Hello there,", followed by a space, then by the user's name. BASIC will allow you to combine (concanate) strings by putting them in the desired order, and putting a plus sign (+) between them. So, we could create the welcome message in BASIC with the statement: WELCOME$ = "Hello there, " + NAME$ Note that there is a space following the comma in the welcome message. This is necessary if a space is desired between the welcome message and the name, as BASIC isn't smart enough to put one there unless you do! Also, note that the variable name (WELCOME$) indicates a string variable, and that the name is pretty descriptive of what the varible is storing. Finally, we want to print the welcome message on the screen. This is done in BASIC by using the PRINT statement. The simplest form (and the one we'll start out with" is the form: PRINT variable-name where variable-name is the variable we want to print. So, our command to print the welcome message would be: PRINT WELCOME$ Finally, we put the whole program together: 10 ' WELCOME.BAS - ask the user to input his/her name, then print it on the 20 ' screen with a nice little welcome message. 30 ' Program from GWBT01.TXT BASIC tutorial file 06/15/1990 40 ' Written by Richard Harper and customized by (your name here!) 50 ' 60 ' Start of main program body: 70 INPUT "What is your name, please"; NAME$ 80 WELCOME$ = "Hello there, " + NAME$ 90 PRINT WELCOME$ 100 END 110 ' End of program - WELCOME.BAS You'll note that I added many comment lines (the lines where the line number is followed by a single quote (') mark) to the program. This is a good idea to do for most programs, as you can give yourself reminders about the program, what it does, and why you wrote it. You can put any information you want into the comment lines, as BASIC will ignore them when the program is run. BASIC refers to these as REM or REMARK lines. Also note that the program ends with a line that says END. This is also a good habit to get into doing, as programs we write later may contain parts following the actual end that only get run in case of unexpected errors (these are called ERROR TRAPPING ROUTINES), or routines that the main program calls to do special tasks (these are called SUBROUTINES). Go ahead and enter this program, and try it out. Whatever you type in as the name will be displayed in the welcome message, with only one exception. Most versions of BASIC (including ours) allow you to ask the user for several input values at once, and these are expected to be seperated by commas (,) so that BASIC can tell which parts go where. As an example: INPUT "What is your name and age", NAME$, AGE would allow you to enter the answer as: Richard Harper,31 (press Enter) and answer both variables at once. BASIC would make Richard Harper one value, and 31 another. It would then look at the INPUT statement, see that Richard Harper is a valid string input, that 31 is a valid numeric input, and put the values in the appropriate variables. If you put a comma in the middle of a single value INPUT statement (like our line 70 above), BASIC tries to split it into two parts. Say you answered the name question as follows: Harper, Richard BASIC would then make Harper one input value, and Richard a second, as they are seperated by commas. It would then see that we ask for only one value (the NAME$ variable), and it would rudely tell you: ?Redo from start and simply print another question mark. There are ways around this, and we will deal with them later. For now, just avoid the use of commas when putting in string information. And we've written our first program! Not as hard as it looked. Try changing around some of the program and see how YOU can customize it to do EXACTLY what you want it to. This is your first taste of the power of BASIC. ******************************************************************************* HOMEWORK ASSIGNMENT: ******************************************************************************* So far, we've seen how to get information from the user, process it, and print it on the screen. For next month's assignment (07/15/1990), we're going to explore some more involved uses of INPUT (LINE INPUT) and PRINT (LOCATE and PRINT USING) to allow the entry of more detailed information, and some ways to make our screen output look a little nicer. Remember: If you have any questions or comments, please feel free to post them in the IBMNEW Message Area #0 addressed to me. Also don't forget that there are almost as many ways of writing a program as there are programmers! The material presented in these tutorials is only one way of many to solve the discussion problems. If you can come up with a better, or easier, or faster way of solving a problem, share it with the rest of us! Next month's tutorial (appearing on or around 07/15/1990, and titled GWBT02.TXT for GW-Basic Tutorial number 2) will include the above discussion topics, along with selected questions or comments from you! Participate and make this a great series! I'll be waiting to hear from everyone as we explore the world of BASIC programming. ----------------end-of-author's-documentation--------------- Software Library Information: This disk copy provided as a service of The Public (Software) Library We are not the authors of this program, nor are we associated with the author in any way other than as a distributor of the program in accordance with the author's terms of distribution. Please direct shareware payments and specific questions about this program to the author of the program, whose name appears elsewhere in this documentation. If you have trouble getting in touch with the author, we will do whatever we can to help you with your questions. All programs have been tested and do run. To report problems, please use the form that is in the file PROBLEM.DOC on many of our disks or in other written for- mat with screen printouts, if possible. The P(s)L cannot de- bug programs over the telephone. Disks in the P(s)L are updated monthly, so if you did not get this disk directly from the P(s)L, you should be aware that the files in this set may no longer be the current versions. For a copy of the latest monthly software library newsletter and a list of the 2,000+ disks in the library, call or write The Public (Software) Library P.O.Box 35705 Houston, TX 77235-5705 (713) 524-6394